package com.expert.handler;


import com.expert.constant.CodeConstant;
import com.expert.constant.MessageConstant;
import com.expert.exception.*;
import com.expert.result.Result;
import io.jsonwebtoken.ClaimJwtException;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author cxs
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler{
    @ExceptionHandler(BaseException.class)
    public Result systemExceptionHandler(BaseException e){
        log.error("异常信息：{}",e.getMessage());
        return Result.error(e.getMessage(), CodeConstant.FAIL);
    }

    @ExceptionHandler(AccessTokenException.class)
    public  Result accessTokenException(AccessTokenException e){
        log.info("AccessToken没用");
        return  Result.error("",CodeConstant.ACCESS_TOKEN_FAIL);
    }
    @ExceptionHandler(AccountLockedException.class)
    public  Result accountLockedException(AccountLockedException e){
        return  Result.error("账号被禁用",CodeConstant.ACCOUNT_LOCK);
    }
    @ExceptionHandler(RefreshTokenException.class)
    public  Result refreshTokenException(RefreshTokenException e){
        log.info(e.getMessage());

        return  Result.error(MessageConstant.USER_NOT_LOGIN,CodeConstant.REFRESH_TOKEN_FAIL);
    }
    @ExceptionHandler(ClaimJwtException.class)
    public  Result claimJwtTokenException(ClaimJwtException e){
        log.info("解析失败");
        return  Result.error(MessageConstant.USER_NOT_LOGIN,CodeConstant.REFRESH_TOKEN_FAIL);
    }
    @ExceptionHandler(SignatureException.class)
    public  Result signatureTokenException(SignatureException e){
        log.info("密钥不匹配");
        return  Result.error(MessageConstant.USER_NOT_LOGIN,CodeConstant.REFRESH_TOKEN_FAIL);
    }
    @ExceptionHandler(InsufficientPromissionException.class)
    public  Result insufficientPromissionException(InsufficientPromissionException e){
        log.info("权限不足异常");
        return  Result.error(MessageConstant.INSUFFICIENT_PERMISSIONS,CodeConstant.INSUFFICIENT_PROMISSION);
    }
}
